﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace YY.DAL
{
	/// <summary>
	/// 数据访问类:agent_users
	/// </summary>
	public partial class agent_users
	{
		public agent_users()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "tb_agent_users"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_agent_users");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(YY.Model.agent_users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tb_agent_users(");
			strSql.Append("user_name,true_name,head_img_url,birthday,sex,registered_time,id_number,e_mail,salt,password,mobile,telphone,area,address,city,registered_ip,amount,member_id,status,is_lock,reason,audit_time,audit_name,check_status,is_submit,documents1,documents2,documents3,documents4,frozen_capital,frozen_capital_time,frozen_pay_time,frozen_thaw_time,remark,sms_account_balance,user_guid)");
			strSql.Append(" values (");
			strSql.Append("@user_name,@true_name,@head_img_url,@birthday,@sex,@registered_time,@id_number,@e_mail,@salt,@password,@mobile,@telphone,@area,@address,@city,@registered_ip,@amount,@member_id,@status,@is_lock,@reason,@audit_time,@audit_name,@check_status,@is_submit,@documents1,@documents2,@documents3,@documents4,@frozen_capital,@frozen_capital_time,@frozen_pay_time,@frozen_thaw_time,@remark,@sms_account_balance,@user_guid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@user_name", SqlDbType.NVarChar,15),
					new SqlParameter("@true_name", SqlDbType.NVarChar,50),
					new SqlParameter("@head_img_url", SqlDbType.NVarChar,200),
					new SqlParameter("@birthday", SqlDbType.DateTime),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@registered_time", SqlDbType.DateTime),
					new SqlParameter("@id_number", SqlDbType.NVarChar,50),
					new SqlParameter("@e_mail", SqlDbType.NVarChar,50),
					new SqlParameter("@salt", SqlDbType.NVarChar,8),
					new SqlParameter("@password", SqlDbType.NVarChar,100),
					new SqlParameter("@mobile", SqlDbType.NVarChar,20),
					new SqlParameter("@telphone", SqlDbType.NVarChar,50),
					new SqlParameter("@area", SqlDbType.NVarChar,50),
					new SqlParameter("@address", SqlDbType.NVarChar,255),
					new SqlParameter("@city", SqlDbType.NVarChar,20),
					new SqlParameter("@registered_ip", SqlDbType.NVarChar,20),
					new SqlParameter("@amount", SqlDbType.NVarChar,50),
					new SqlParameter("@member_id", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@is_lock", SqlDbType.Int,4),
					new SqlParameter("@reason", SqlDbType.NVarChar,300),
					new SqlParameter("@audit_time", SqlDbType.DateTime),
					new SqlParameter("@audit_name", SqlDbType.NVarChar,50),
					new SqlParameter("@check_status", SqlDbType.Int,4),
					new SqlParameter("@is_submit", SqlDbType.Int,4),
					new SqlParameter("@documents1", SqlDbType.Text),
					new SqlParameter("@documents2", SqlDbType.Text),
					new SqlParameter("@documents3", SqlDbType.Text),
					new SqlParameter("@documents4", SqlDbType.Text),
					new SqlParameter("@frozen_capital", SqlDbType.NVarChar,50),
					new SqlParameter("@frozen_capital_time", SqlDbType.DateTime),
					new SqlParameter("@frozen_pay_time", SqlDbType.DateTime),
					new SqlParameter("@frozen_thaw_time", SqlDbType.DateTime),
					new SqlParameter("@remark", SqlDbType.Text),
					new SqlParameter("@sms_account_balance", SqlDbType.NVarChar,50),
					new SqlParameter("@user_guid", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = model.user_name;
			parameters[1].Value = model.true_name;
			parameters[2].Value = model.head_img_url;
			parameters[3].Value = model.birthday;
			parameters[4].Value = model.sex;
			parameters[5].Value = model.registered_time;
			parameters[6].Value = model.id_number;
			parameters[7].Value = model.e_mail;
			parameters[8].Value = model.salt;
			parameters[9].Value = model.password;
			parameters[10].Value = model.mobile;
			parameters[11].Value = model.telphone;
			parameters[12].Value = model.area;
			parameters[13].Value = model.address;
			parameters[14].Value = model.city;
			parameters[15].Value = model.registered_ip;
			parameters[16].Value = model.amount;
			parameters[17].Value = model.member_id;
			parameters[18].Value = model.status;
			parameters[19].Value = model.is_lock;
			parameters[20].Value = model.reason;
			parameters[21].Value = model.audit_time;
			parameters[22].Value = model.audit_name;
			parameters[23].Value = model.check_status;
			parameters[24].Value = model.is_submit;
			parameters[25].Value = model.documents1;
			parameters[26].Value = model.documents2;
			parameters[27].Value = model.documents3;
			parameters[28].Value = model.documents4;
			parameters[29].Value = model.frozen_capital;
			parameters[30].Value = model.frozen_capital_time;
			parameters[31].Value = model.frozen_pay_time;
			parameters[32].Value = model.frozen_thaw_time;
			parameters[33].Value = model.remark;
			parameters[34].Value = model.sms_account_balance;
			parameters[35].Value = model.user_guid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(YY.Model.agent_users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_agent_users set ");
			strSql.Append("user_name=@user_name,");
			strSql.Append("true_name=@true_name,");
			strSql.Append("head_img_url=@head_img_url,");
			strSql.Append("birthday=@birthday,");
			strSql.Append("sex=@sex,");
			strSql.Append("registered_time=@registered_time,");
			strSql.Append("id_number=@id_number,");
			strSql.Append("e_mail=@e_mail,");
			strSql.Append("salt=@salt,");
			strSql.Append("password=@password,");
			strSql.Append("mobile=@mobile,");
			strSql.Append("telphone=@telphone,");
			strSql.Append("area=@area,");
			strSql.Append("address=@address,");
			strSql.Append("city=@city,");
			strSql.Append("registered_ip=@registered_ip,");
			strSql.Append("amount=@amount,");
			strSql.Append("member_id=@member_id,");
			strSql.Append("status=@status,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("reason=@reason,");
			strSql.Append("audit_time=@audit_time,");
			strSql.Append("audit_name=@audit_name,");
			strSql.Append("check_status=@check_status,");
			strSql.Append("is_submit=@is_submit,");
			strSql.Append("documents1=@documents1,");
			strSql.Append("documents2=@documents2,");
			strSql.Append("documents3=@documents3,");
			strSql.Append("documents4=@documents4,");
			strSql.Append("frozen_capital=@frozen_capital,");
			strSql.Append("frozen_capital_time=@frozen_capital_time,");
			strSql.Append("frozen_pay_time=@frozen_pay_time,");
			strSql.Append("frozen_thaw_time=@frozen_thaw_time,");
			strSql.Append("remark=@remark,");
			strSql.Append("sms_account_balance=@sms_account_balance,");
			strSql.Append("user_guid=@user_guid");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@user_name", SqlDbType.NVarChar,15),
					new SqlParameter("@true_name", SqlDbType.NVarChar,50),
					new SqlParameter("@head_img_url", SqlDbType.NVarChar,200),
					new SqlParameter("@birthday", SqlDbType.DateTime),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@registered_time", SqlDbType.DateTime),
					new SqlParameter("@id_number", SqlDbType.NVarChar,50),
					new SqlParameter("@e_mail", SqlDbType.NVarChar,50),
					new SqlParameter("@salt", SqlDbType.NVarChar,8),
					new SqlParameter("@password", SqlDbType.NVarChar,100),
					new SqlParameter("@mobile", SqlDbType.NVarChar,20),
					new SqlParameter("@telphone", SqlDbType.NVarChar,50),
					new SqlParameter("@area", SqlDbType.NVarChar,50),
					new SqlParameter("@address", SqlDbType.NVarChar,255),
					new SqlParameter("@city", SqlDbType.NVarChar,20),
					new SqlParameter("@registered_ip", SqlDbType.NVarChar,20),
					new SqlParameter("@amount", SqlDbType.NVarChar,50),
					new SqlParameter("@member_id", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@is_lock", SqlDbType.Int,4),
					new SqlParameter("@reason", SqlDbType.NVarChar,300),
					new SqlParameter("@audit_time", SqlDbType.DateTime),
					new SqlParameter("@audit_name", SqlDbType.NVarChar,50),
					new SqlParameter("@check_status", SqlDbType.Int,4),
					new SqlParameter("@is_submit", SqlDbType.Int,4),
					new SqlParameter("@documents1", SqlDbType.Text),
					new SqlParameter("@documents2", SqlDbType.Text),
					new SqlParameter("@documents3", SqlDbType.Text),
					new SqlParameter("@documents4", SqlDbType.Text),
					new SqlParameter("@frozen_capital", SqlDbType.NVarChar,50),
					new SqlParameter("@frozen_capital_time", SqlDbType.DateTime),
					new SqlParameter("@frozen_pay_time", SqlDbType.DateTime),
					new SqlParameter("@frozen_thaw_time", SqlDbType.DateTime),
					new SqlParameter("@remark", SqlDbType.Text),
					new SqlParameter("@sms_account_balance", SqlDbType.NVarChar,50),
					new SqlParameter("@user_guid", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.user_name;
			parameters[1].Value = model.true_name;
			parameters[2].Value = model.head_img_url;
			parameters[3].Value = model.birthday;
			parameters[4].Value = model.sex;
			parameters[5].Value = model.registered_time;
			parameters[6].Value = model.id_number;
			parameters[7].Value = model.e_mail;
			parameters[8].Value = model.salt;
			parameters[9].Value = model.password;
			parameters[10].Value = model.mobile;
			parameters[11].Value = model.telphone;
			parameters[12].Value = model.area;
			parameters[13].Value = model.address;
			parameters[14].Value = model.city;
			parameters[15].Value = model.registered_ip;
			parameters[16].Value = model.amount;
			parameters[17].Value = model.member_id;
			parameters[18].Value = model.status;
			parameters[19].Value = model.is_lock;
			parameters[20].Value = model.reason;
			parameters[21].Value = model.audit_time;
			parameters[22].Value = model.audit_name;
			parameters[23].Value = model.check_status;
			parameters[24].Value = model.is_submit;
			parameters[25].Value = model.documents1;
			parameters[26].Value = model.documents2;
			parameters[27].Value = model.documents3;
			parameters[28].Value = model.documents4;
			parameters[29].Value = model.frozen_capital;
			parameters[30].Value = model.frozen_capital_time;
			parameters[31].Value = model.frozen_pay_time;
			parameters[32].Value = model.frozen_thaw_time;
			parameters[33].Value = model.remark;
			parameters[34].Value = model.sms_account_balance;
			parameters[35].Value = model.user_guid;
			parameters[36].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_agent_users ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_agent_users ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public YY.Model.agent_users GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,user_name,true_name,head_img_url,birthday,sex,registered_time,id_number,e_mail,salt,password,mobile,telphone,area,address,city,registered_ip,amount,member_id,status,is_lock,reason,audit_time,audit_name,check_status,is_submit,documents1,documents2,documents3,documents4,frozen_capital,frozen_capital_time,frozen_pay_time,frozen_thaw_time,remark,sms_account_balance,user_guid from tb_agent_users ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			YY.Model.agent_users model=new YY.Model.agent_users();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public YY.Model.agent_users DataRowToModel(DataRow row)
		{
			YY.Model.agent_users model=new YY.Model.agent_users();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["user_name"]!=null)
				{
					model.user_name=row["user_name"].ToString();
				}
				if(row["true_name"]!=null)
				{
					model.true_name=row["true_name"].ToString();
				}
				if(row["head_img_url"]!=null)
				{
					model.head_img_url=row["head_img_url"].ToString();
				}
				if(row["birthday"]!=null && row["birthday"].ToString()!="")
				{
					model.birthday=DateTime.Parse(row["birthday"].ToString());
				}
				if(row["sex"]!=null && row["sex"].ToString()!="")
				{
					model.sex=int.Parse(row["sex"].ToString());
				}
				if(row["registered_time"]!=null && row["registered_time"].ToString()!="")
				{
					model.registered_time=DateTime.Parse(row["registered_time"].ToString());
				}
				if(row["id_number"]!=null)
				{
					model.id_number=row["id_number"].ToString();
				}
				if(row["e_mail"]!=null)
				{
					model.e_mail=row["e_mail"].ToString();
				}
				if(row["salt"]!=null)
				{
					model.salt=row["salt"].ToString();
				}
				if(row["password"]!=null)
				{
					model.password=row["password"].ToString();
				}
				if(row["mobile"]!=null)
				{
					model.mobile=row["mobile"].ToString();
				}
				if(row["telphone"]!=null)
				{
					model.telphone=row["telphone"].ToString();
				}
				if(row["area"]!=null)
				{
					model.area=row["area"].ToString();
				}
				if(row["address"]!=null)
				{
					model.address=row["address"].ToString();
				}
				if(row["city"]!=null)
				{
					model.city=row["city"].ToString();
				}
				if(row["registered_ip"]!=null)
				{
					model.registered_ip=row["registered_ip"].ToString();
				}
				if(row["amount"]!=null)
				{
					model.amount=row["amount"].ToString();
				}
				if(row["member_id"]!=null && row["member_id"].ToString()!="")
				{
					model.member_id=int.Parse(row["member_id"].ToString());
				}
				if(row["status"]!=null && row["status"].ToString()!="")
				{
					model.status=int.Parse(row["status"].ToString());
				}
				if(row["is_lock"]!=null && row["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(row["is_lock"].ToString());
				}
				if(row["reason"]!=null)
				{
					model.reason=row["reason"].ToString();
				}
				if(row["audit_time"]!=null && row["audit_time"].ToString()!="")
				{
					model.audit_time=DateTime.Parse(row["audit_time"].ToString());
				}
				if(row["audit_name"]!=null)
				{
					model.audit_name=row["audit_name"].ToString();
				}
				if(row["check_status"]!=null && row["check_status"].ToString()!="")
				{
					model.check_status=int.Parse(row["check_status"].ToString());
				}
				if(row["is_submit"]!=null && row["is_submit"].ToString()!="")
				{
					model.is_submit=int.Parse(row["is_submit"].ToString());
				}
				if(row["documents1"]!=null)
				{
					model.documents1=row["documents1"].ToString();
				}
				if(row["documents2"]!=null)
				{
					model.documents2=row["documents2"].ToString();
				}
				if(row["documents3"]!=null)
				{
					model.documents3=row["documents3"].ToString();
				}
				if(row["documents4"]!=null)
				{
					model.documents4=row["documents4"].ToString();
				}
				if(row["frozen_capital"]!=null)
				{
					model.frozen_capital=row["frozen_capital"].ToString();
				}
				if(row["frozen_capital_time"]!=null && row["frozen_capital_time"].ToString()!="")
				{
					model.frozen_capital_time=DateTime.Parse(row["frozen_capital_time"].ToString());
				}
				if(row["frozen_pay_time"]!=null && row["frozen_pay_time"].ToString()!="")
				{
					model.frozen_pay_time=DateTime.Parse(row["frozen_pay_time"].ToString());
				}
				if(row["frozen_thaw_time"]!=null && row["frozen_thaw_time"].ToString()!="")
				{
					model.frozen_thaw_time=DateTime.Parse(row["frozen_thaw_time"].ToString());
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
				if(row["sms_account_balance"]!=null)
				{
					model.sms_account_balance=row["sms_account_balance"].ToString();
				}
				if(row["user_guid"]!=null && row["user_guid"].ToString()!="")
				{
					model.user_guid= new Guid(row["user_guid"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,user_name,true_name,head_img_url,birthday,sex,registered_time,id_number,e_mail,salt,password,mobile,telphone,area,address,city,registered_ip,amount,member_id,status,is_lock,reason,audit_time,audit_name,check_status,is_submit,documents1,documents2,documents3,documents4,frozen_capital,frozen_capital_time,frozen_pay_time,frozen_thaw_time,remark,sms_account_balance,user_guid ");
			strSql.Append(" FROM tb_agent_users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,user_name,true_name,head_img_url,birthday,sex,registered_time,id_number,e_mail,salt,password,mobile,telphone,area,address,city,registered_ip,amount,member_id,status,is_lock,reason,audit_time,audit_name,check_status,is_submit,documents1,documents2,documents3,documents4,frozen_capital,frozen_capital_time,frozen_pay_time,frozen_thaw_time,remark,sms_account_balance,user_guid ");
			strSql.Append(" FROM tb_agent_users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_agent_users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from tb_agent_users T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_agent_users";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

